/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.scs.ubbcluj.model;

import java.io.Serializable;

/**
 *
 * @author Torok
 */
public class Attribute implements Model, Serializable {

    private static final String TYPE = "ATTR";
    private String name;
    private String tip;
    private int length;
    private boolean isNull;
    private boolean isUnique;

    public Attribute(String name, String tip, int length, boolean isNull) {
        this.name = name;
        this.tip = tip;
        this.length = length;
        this.isNull = isNull;
        this.isUnique = false;
    }

    public Attribute(String name, String tip, int length, boolean isNull, boolean isUnique) {
        this.name = name;
        this.tip = tip;
        this.length = length;
        this.isNull = isNull;
        this.isUnique = isUnique;
    }

    public String getTip() {
        return tip;
    }

    public void setTip(String tip) {
        this.tip = tip;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public boolean isIsNull() {
        return isNull;
    }

    public void setIsNull(boolean isNull) {
        this.isNull = isNull;
    }

    public boolean isUnique() {
        return isUnique;
    }

    public void setUnique(boolean unique) {
        this.isUnique = unique;
    }

    @Override
    public String getType() {
        return TYPE;
    }

    @Override
    public String toString() {
        return this.name + ":" + this.tip;
    }

    public String checkAttributeValue(String str) {
        //check type
        if (str.length() != 0) {
            if (this.getTip().equals("char")) {
                if (str.length() != 1) {
                    return str + " is not a char! (Type of column " + this.getName() + " is char.";
                }

            } else if (this.getTip().equals("short")) {
                try {
                    Short.parseShort(str);
                } catch (NumberFormatException e) {
                    return str + " is not a short! (Type of column " + this.getName() + " is short.";
                }
            } else if (this.getTip().equals("int")) {
                try {
                    Integer.parseInt(str);
                } catch (NumberFormatException e) {
                    return str + " is not an integer! (Type of column " + this.getName() + " is integer.";
                }
            } 
        }
        //check length
        if (this.getLength() < str.length()) {
            return "The length of " + str + "is greater than the maximum length of column " + this.getName() + ": " + this.getLength();
        }
        //check isNull
        if (!this.isIsNull() && str.trim().length() == 0) {
            return "Column " + this.getName() + " can not be null!";
        }

        return null;
    }
}
